package com.football.springboot.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.football.springboot.common.Constants;
import com.football.springboot.common.Result;
import com.football.springboot.entity.Dict;
import com.football.springboot.mapper.DictMapper;
import com.football.springboot.vo.MenuQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.football.springboot.common.Return;

//导入service
import com.football.springboot.service.IMenuService;
//导入entity
import com.football.springboot.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李越
 * @since 2023-02-16
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @Resource
    private DictMapper dictMapper;

    // 新增或者更新
    @ApiOperation("新增或者更新菜单")
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }

    //删除
    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    //批量删除
    @ApiOperation("批量删除")
    @PostMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        menuService.removeByIds(ids);
        return Result.success();
    }

    //查询全部(不用这个)
    @ApiOperation("查询全部(不用这个)")
    @GetMapping("/findall")
    public Result findAll() {
        return Result.success(menuService.list());
    }


    //查询全部菜单
    @ApiOperation("查询全部菜单")
    @GetMapping("/findMenus")
    public Result findMenus(@RequestParam(defaultValue = "") String name) {
        return Result.success(menuService.findMenus(name));
    }


    //根据id查找菜单信息
    @ApiOperation("根据id查找菜单信息")
    @GetMapping("/getMenu/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/pageMenu")
    public Return pageMenu(@RequestParam Integer current,
                           @RequestParam Integer limit,
                           MenuQueryVo menuQueryVo) {

        //创建page对象,传递当前页数current,每页记录数limit
        Page<Menu> page = new Page<>(current,limit);
        //构建条件 模糊查询
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        String name = menuQueryVo.getName();

        //菜单名查询
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("id");

        Page<Menu> pageMenu = menuService.page(page,queryWrapper);

        return Return.ok().put("pageMenu",pageMenu).put("total",pageMenu.getTotal());
    }


    // 获取图标
    @ApiOperation("获取图标")
    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(queryWrapper));
    }


}

